package com.handpay.zztong.hp.swiper.ic;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * 根据CUPMobile规范和输入的元素标识和取值，生成XML 远程支付报文接口
 * 
 * @author jcao
 * 
 */
public class CUPMobileXML
{
	private Document			document;
	// Element
	private Element				cupMobile, transaction, merchant, terminal,
			order, transAmount, billAmount, accountBalance, creditAcctBal,
			merchantAuthentication;				// 支付应用名称
	// 支付应用名称取�?
	public final static String	UPCards		= "UPCards", UPCashDC = "UPCashDC";
	public final static String	SEPARTOR	= "|";								// 分隔�?
																				
	public CUPMobileXML()
	{
		document = DocumentHelper.createDocument();
		document.setXMLEncoding("utf-8");
		cupMobile = document.addElement("cupMobile");
	}
	
	public String getXML()
	{
		return document.asXML().replaceAll("\n", "");
	}
	
	/**
	 * 增加Element/Attribute/Value
	 * 
	 * @param elementId
	 * @param value
	 */
	public void add(int elementId, String value)
	{
		if (value == null || value.length() < 1)
		{
			return;
		}
		checkElement(elementId);
		switch (elementId)
		{
			case 1:// 支付应用名称cupMobile:application
				cupMobile.addAttribute("application", value);
				break;
			case 2:// 版本号cupMobile:version
				cupMobile.addAttribute("version", value);
				break;
			case 3:// 交易类型cupMobile.transaction:type
				transaction.addAttribute("type", value);
				break;
			case 4:// 交易提交时间cupMobile.transaction.submitTime
				transaction.addElement("submitTime").setText(value);
				break;
			case 5:// 商户代码cupMobile.transaction.merchant:id
				merchant.addAttribute("id", value);
				break;
			case 6:// 商户名称cupMobile.transaction.merchant:name
				merchant.addAttribute("name", value);
				break;
			case 7:// 商户国家代码cupMobile.transaction.merchant:country
				merchant.addAttribute("country", value);
				break;
			case 8:// 子商�?终端代码cupMobile.transaction.terminal:id
				terminal.addAttribute("id", value);
				break;
			case 9:// 订单号cupMobile.transaction.order:id
				order.addAttribute("id", value);
				break;
			case 10:// 清算日期cupMobile.transaction.settleDate
				transaction.addElement("settleDate").setText(value);
				break;
			case 11:// 交易金额cupMobile.transaction.transAmount
				transAmount.addText(value);
				break;
			case 12:// 交易币种cupMobile.transaction.transAmount:currency
				transAmount.addAttribute("currency", value);
				break;
			case 13:// 扣账金额cupMobile.transaction.billAmount
				billAmount.addText(value);
				break;
			case 14:// 扣账币种cupMobile.transaction.billAmount:currency
				billAmount.addAttribute("currency", value);
				break;
			case 15:// 扣账汇率cupMobile.transaction.billAmount:convertRate
				billAmount.addAttribute("convertRate", value);
				break;
			case 16:// 账户1cupMobile.transaction.accountNumber1
				transaction.addElement("accountNumber1").setText(value);
				break;
			case 17:// 个人身份标识码cupMobile.transaction.pin
				transaction.addElement("pin").setText(value);
				break;
			case 20:// 原始交易提交时间cupMobile.transaction.origSubmitTime
				transaction.addElement("origSubmitTime").setText(value);
				break;
			case 25:// 磁道2数据cupMobile.transaction.track2Data
				transaction.addElement("track2Data").setText(value);
				break;
			case 26:// 磁道3数据cupMobile.transaction.track3Data
				transaction.addElement("track3Data").setText(value);
				break;
			case 27:// 持卡人身份信息cupMobile.transaction.cardholderIdInfo
				transaction.addElement("cardholderIdInfo").setText(value);
				break;
			case 28:// 终端芯片序列号cupMobile.transaction.cardSerialNumber
				transaction.addElement("cardSerialNumber").setText(value);
				break;
			case 29:// CMSP保留使用cupMobile.transaction.cmspReserved
				transaction.addElement("cmspReserved").setText(value);
				break;
			case 30:// 账户余额cupMobile.transaction.accountBalance
				accountBalance.addText(value);
				break;
			case 31:// 账户余额币种cupMobile.transaction.accountBalance:currency
				accountBalance.addAttribute("currency", value);
				break;
			case 32:// 电子钱包数据cupMobile.transaction.upCashData
				transaction.addElement("upCashData").setText(value);
				break;
			case 33:// 帐户2cupMobile.transaction.accountNumber2
				transaction.addElement("accountNumber2").setText(value);
				break;
			case 34:// 卡有效期cupMobile.transaction.expireDate
				transaction.addElement("expireDate").setText(value);
				break;
			case 35:// 服务信息cupMobile.transaction.serviceInfo
				transaction.addElement("serviceInfo").setText(value);
				break;
			case 36:// 动�?密钥数据cupMobile.transaction.dynamicKeyData
				transaction.addElement("dynamicKeyData").setText(value);
				break;
			case 37:// 安全载体类型cupMobile.transaction.securityChipTp
				transaction.addElement("securityChipTp").setText(value);
				break;
			case 38:// SP代码cupMobile.transaction.spId
				transaction.addElement("SpId").setText(value);
				break;
			case 39:// 卡片序列号cupMobile.transaction.ICNumber
				transaction.addElement("ICNumber").setText(value);
				break;
			case 40:// 系统支持方cupMobile.transaction.SysProvide
				transaction.addElement("SysProvide").setText(value);
				break;
			case 41:// 原交易流水号cupMobile.transaction.OrgTransSerNo
				transaction.addElement("OrgTransSerNo").setText(value);
				break;
			case 42:// 信用卡账户余额cupMobile.transaction.creditAcctBal
				creditAcctBal.addText(value);
				break;
			case 43:// 信用卡账户余额币种cupMobile.transaction.creditAcctBal:currency
				creditAcctBal.addAttribute("currency", value);
				break;
			case 251:// 借贷记应用数据cupMobile.transaction.DCData
				transaction.addElement("DCData").setText(value);
				break;
			case 252:// 商户摘要数据cupMobile.merchantAuthentication.digest
				merchantAuthentication.addElement("digest").setText(value);
				break;
			case 253:// 商户数字签名cupMobile.merchantAuthentication.signature
				merchantAuthentication.addElement("signature").setText(value);
				break;
			case 254:// 发�?方数字签名cupMobile.senderSignature
				cupMobile.addElement("senderSignature").setText(value);
				break;
			case 255:// 报文鉴别码cupMobile.mac
				cupMobile.addElement("mac").setText(value);
				break;
		}
	}
	
	private void checkElement(int elementId)
	{
		if (elementId >= 3 && elementId <= 251 && transaction == null)
		{
			transaction = cupMobile.addElement("transaction");
		}
		if (elementId >= 5 && elementId <= 7 && merchant == null)
		{
			merchant = transaction.addElement("merchant");
		}
		if (elementId == 8 && terminal == null)
		{
			terminal = transaction.addElement("terminal");
		}
		if (elementId == 9 && order == null)
		{
			order = transaction.addElement("order");
		}
		if (elementId >= 11 && elementId <= 12 && transAmount == null)
		{
			transAmount = transaction.addElement("transAmount");
		}
		if (elementId >= 13 && elementId <= 15 && billAmount == null)
		{
			billAmount = transaction.addElement("billAmount");
		}
		if (elementId >= 30 && elementId <= 31 && accountBalance == null)
		{
			accountBalance = transaction.addElement("accountBalance");
		}
		if (elementId >= 42 && elementId <= 43 && creditAcctBal == null)
		{
			creditAcctBal = transaction.addElement("creditAcctBal");
		}
		if (elementId >= 252 && elementId <= 253
				&& merchantAuthentication == null)
		{
			merchantAuthentication = cupMobile
					.addElement("merchantAuthentication");
		}
	}
	
	// private String transAmount(String amount)
	// {
	// String v = "0000000000000" + amount;
	// int index = v.indexOf(".");
	// if(index > 0)
	// {
	// v = v.substring(0, index) + v.substring(index + 1);
	// }
	// else if(index < 0)
	// {
	// v += "00";
	// }
	// return v.substring(v.length() - 12);
	// }
}
